package com.smc.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.smc.dao.UserDao;
import com.smc.model.User;
 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.UUID;

@Controller
public class UserAction {
    @Autowired
    private UserDao userDao;
    @RequestMapping("/addUser")
    public void add(HttpServletRequest request, HttpServletResponse response){
        String id= UUID.randomUUID().toString();
        String name=request.getParameter("name");
        String acct=request.getParameter("acct");
        String phone=request.getParameter("phone");
        String pass=request.getParameter("pass");
        String str1=request.getParameter("str1");
        String str2=request.getParameter("str2");
        String str3=request.getParameter("str3");
        User user=new User();
        user.setId(id);
        user.setName(name);
        user.setAcct(acct);
        user.setPass(pass);
        user.setPhone(phone);
        user.setStr1(str1);
        user.setStr2(str2);
        user.setStr3(str3);
        userDao.insert(user);
        try {
            response.getWriter().println("0000");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @RequestMapping("/findUser")
    public void find(HttpServletRequest request, HttpServletResponse response){
            String start=request.getParameter("start");
            String rownum=request.getParameter("rownum");
            HashMap<String,String> map=new HashMap<String,String>();
            map.put("name",request.getParameter("name"));
            map.put("acct",request.getParameter("acct"));
            map.put("phone",request.getParameter("phone"));
        if(start!=null&&rownum!=null) {
             PageInfo pageInfo = userDao.selectPage(Integer.parseInt(start), Integer.parseInt(rownum), map);
            String jsonString = JSON.toJSONString(pageInfo);
            System.out.println(jsonString);
            try {
                response.getWriter().println(jsonString);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @RequestMapping("/deleteUser")
    public void delete(HttpServletRequest request ,HttpServletResponse response){
        String id=request.getParameter("id");
        userDao.delete(id);
        try {
            response.getWriter().println("0000");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @RequestMapping("/updateUser")
    public void update(HttpServletRequest request, HttpServletResponse response){
        String id= request.getParameter("id");
        String name=request.getParameter("name");
        String acct=request.getParameter("acct");
        String phone=request.getParameter("phone");
        String pass=request.getParameter("pass");
        String str1=request.getParameter("str1");
        String str2=request.getParameter("str2");
        String str3=request.getParameter("str3");
        User user=new User();
        user.setId(id);
        user.setName(name);
        user.setAcct(acct);
        user.setPass(pass);
        user.setPhone(phone);
        user.setStr1(str1);
        user.setStr2(str2);
        user.setStr3(str3);
        userDao.update(user);
        try {
            response.getWriter().println("0000");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
